//
//  BeFondViewController.m
//  AILove
//
//  Created by 甘世清 on 2023/12/16.
//

#import "BeFondViewController.h"
#import "ViewController.h"
@interface BeFondViewController ()
@property (nonatomic ,strong) SQCustomButton * manHeader;
@property (nonatomic ,strong) SQCustomButton * womanHeader;
@end

@implementation BeFondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navLeftBtn.hidden = YES;
    [self.jsonData setObject:@"2" forKey:@"gender"];
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{
    UIImageView * image =  [[UIImageView alloc]init];
     image.image = SQGetImage(@"背景1");
     image.contentMode = UIViewContentModeScaleAspectFill;
     [self.view addSubview:image];
     [image mas_makeConstraints:^(MASConstraintMaker *make) {
         make.edges.equalTo(self.view);
     }];
    UILabel * label = [[UILabel alloc]init];
    label.textColor = UIColor.whiteColor;
    label.font = FONT_BOLD(24);
    label.numberOfLines = 0;
    label.text = SQNSLocalString(@"最后一步\n选择你喜欢的虚拟角色性别");
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(100);
    }];
    [self.view addSubview:self.manHeader];
    [self.manHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(240);
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.view).offset(210);
    }];
    [self.view addSubview:self.womanHeader];
    [self.womanHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(240);
        make.right.equalTo(self.view.mas_right).offset(-30);
        make.top.equalTo(self.view).offset(290);
    }];
    WS(weakSelf);
    [self.manHeader setTouchBlock:^(SQCustomButton * _Nonnull button) {
        button.SQTouchBtn.selected = !button.SQTouchBtn.selected;
        if (button.SQTouchBtn.selected) {
            button.layer.borderColor = SQColor(@"#F531FC").CGColor;
        } else {
            button.layer.borderColor = UIColor.clearColor.CGColor;
        }
        weakSelf.womanHeader.layer.borderColor = UIColor.clearColor.CGColor;
        weakSelf.womanHeader.SQTouchBtn.selected = NO;
        [weakSelf.jsonData setObject:@"0" forKey:@"gender"];
    }];
    
    [self.womanHeader setTouchBlock:^(SQCustomButton * _Nonnull button) {
        button.SQTouchBtn.selected = !button.SQTouchBtn.selected;
        if (button.SQTouchBtn.selected) {
            button.layer.borderColor = SQColor(@"#F531FC").CGColor;
        } else {
            button.layer.borderColor = UIColor.clearColor.CGColor;
        }
        weakSelf.manHeader.layer.borderColor = UIColor.clearColor.CGColor;
        weakSelf.manHeader.SQTouchBtn.selected = NO;
        [weakSelf.jsonData setObject:@"1" forKey:@"gender"];
    }];
    
    UILabel * nicheng = [[UILabel alloc]init];
    nicheng.text = SQNSLocalString(@"不选择默认全部");
    nicheng.font = FONT_MEDIUM(16);
    nicheng.textColor = SQColor(@"#B4B4B6");
    [self.view addSubview:nicheng];
    [nicheng mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-180);
        make.centerX.equalTo(self.view);
    }];
    
    HeaderView * btn = [[HeaderView alloc]init];
    btn.font = FONT_BOLD(16);
    btn.text = SQNSLocalString(@"下一步");
    btn.layer.cornerRadius = 25;
    btn.headerImage.image = SQGetImage(@"按钮背景");
    [btn.btn addTarget:self action:@selector(SQ_buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-80);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.centerX.equalTo(self.view);
    }];
}
-(void)SQ_buttonClick{
    WS(weakSelf);
    if (self.image) {
        //上传头像
        [YJProgressHUD SQ_showMsgWithoutView:@""];
        [HttpTool SQ_Image:self.image successBlock:^(BaseModel * _Nonnull httpModel) {
            [weakSelf SQ_setUpName:httpModel.data];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    } else {
        [weakSelf SQ_setUpName:nil];
    }
  
}
-(void)SQ_setUpName:(NSString*)header{
    if ([header isNotEmpty]) {
        [self.jsonData setObject:header forKey:@"portrait"];
    }
    [HttpTool post:Url5 paramDic:self.jsonData successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [ UserModel share];
        model.portrait = header;
        model.name = [self.jsonData objectForKey:@"name"];
        model.gender = [self.jsonData objectForKey:@"gender"];
        [UserModel updateLocalUserModel:model];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init]];
         self.view.window.rootViewController = nav;
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (SQCustomButton *)manHeader{
    if (!_manHeader) {
        _manHeader = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(130, 240) midmargin:0];
        _manHeader.layer.borderColor = UIColor.clearColor.CGColor;
        _manHeader.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _manHeader.clipsToBounds = YES;
        _manHeader.SQTouchBtn.selected = NO;
        _manHeader.layer.borderWidth = 2;
        _manHeader.layer.cornerRadius = 8;
        _manHeader.image = SQGetImage(@"矩形 20");
    }
    return _manHeader;
}
- (SQCustomButton *)womanHeader{
    if (!_womanHeader) {
        _womanHeader = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(130, 240) midmargin:0];
        _womanHeader.SQTouchBtn.selected = NO;
        _womanHeader.layer.borderColor = UIColor.clearColor.CGColor;
        _womanHeader.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _womanHeader.clipsToBounds = YES;
        _womanHeader.layer.borderWidth = 2;
        _womanHeader.layer.cornerRadius = 8;
        _womanHeader.image = SQGetImage(@"矩形 19");
    }
    return _womanHeader;
}
@end
